/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.DBExecutor;
import net.iwsh.core.db.ext.DialectSyntaxParser;
import net.iwsh.core.db.ext.SQLWithParams;
import net.iwsh.core.utils.IDGenerator;

import com.us.usws.model.domain.userevent.UserEvent;
import com.us.usws.model.flat.userevents.DOUserEvent;

/**
 * DAO class
 * @author robot
 */
public class UserEventDAO {

	/**
	 * Query SQLs
	 */
	private static final String SQL_GET_TOTAL_COUNT = "select count(*) from hv_user_event";

	private static final String SQL_GET_ALL_USEREVENTS = "select * from hv_user_event";

	private static final String SQL_GET_USEREVENT_BY_PK = "select * from hv_user_event where uid = ?";

	private static final String SQL_GET_EVETS_BY_USER_OPTR = "select * from hv_user_event where user_uid=? and optr_type_dsid=?";

	/**
	 * CUR SQLs
	 */
	private static final String SQL_INSERT_USEREVENT = "insert into hv_user_event(uid,user_uid,event_date,optr_type_dsid,event_type,event_id,resource_id,optr_detail,view_level,view_target,description) values (?,?,?,?,?,?,?,?,?,?,?)";

	private static final String SQL_UPDATE_USEREVENT = "update hv_user_event set user_uid=?, event_date=?, optr_type_dsid=?, event_type=?, event_id=?, resource_id=?, optr_detail=?, view_level=?, view_target=?, description=? where uid=?";

	private static final String SQL_DEL_USEREVENT = "delete from hv_user_event where uid=?";

	/**
	 * DBExeutor
	 */
	private DBExecutor dbe = new DBExecutor();

	/**
	 * Get Count.
	 */
	public int getTotalCount() {
		return dbe.getCount(SQL_GET_TOTAL_COUNT);
	}

	/**
	 * Get all list.
	 */
	public List<UserEvent> getAllUserEvents() {
		return dbe.list(SQL_GET_ALL_USEREVENTS, UserEvent.class);
	}

	/**
	 * Get all list by pagination.
	 */
	public List<UserEvent> getUserEvents(int start, int end) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_ALL_USEREVENTS, null, start, end);
		return dbe.list(swp.getSql(), UserEvent.class, swp.getParams());
	}

	/**
	 * Get UserEvent by primary key.
	 */
	public UserEvent getUserEvent(String uid) {
		UserEvent u = dbe.load(SQL_GET_USEREVENT_BY_PK, UserEvent.class, uid);
		return u;
	}

	/**
	 * Insert one row.
	 */
	public String insertUserEvent(DOUserEvent userEvent) {
		String uid = IDGenerator.getInstance().genUID();
		userEvent.setUid(uid);
		dbe.execute(SQL_INSERT_USEREVENT, userEvent.getUid(), userEvent.getUserUid(), userEvent.getEventDate(),
				userEvent.getOptrTypeDsid(), userEvent.getEventType(), userEvent.getEventId(), userEvent.getResourceId(),
				userEvent.getOptrDetail(), userEvent.getViewLevel(), userEvent.getViewTarget(), userEvent.getDescription());
		return uid;
	}

	/**
	 * Update by PK.
	 */
	public void updateUserEvent(DOUserEvent userEvent) {
		dbe.execute(SQL_UPDATE_USEREVENT, userEvent.getUserUid(), userEvent.getEventDate(), userEvent.getOptrTypeDsid(),
				userEvent.getEventType(), userEvent.getEventId(), userEvent.getResourceId(), userEvent.getOptrDetail(),
				userEvent.getViewLevel(), userEvent.getViewTarget(), userEvent.getDescription(), userEvent.getUid());
	}

	/**
	 * Delete row by PK.
	 */
	public void deleteUserEvent(String uid) {
		dbe.execute(SQL_DEL_USEREVENT, uid);
	}

	/**
	 * get event count
	 * @param userUid
	 * @param optrType
	 * @return
	 */
	public int getUserEventCountByUser(String userUid, int optrType) {
		String sql = DialectSyntaxParser.getParser().getCountSQL(SQL_GET_EVETS_BY_USER_OPTR);
		return dbe.getCount(sql, userUid, optrType);
	}

	/**
	 * get events by user, with pagination
	 * @param userUid
	 * @param optrType
	 * @param start
	 * @param pageSize
	 * @return
	 */
	public List<UserEvent> getUserEventsByUser(String userUid, int optrType, int start, int pageSize) {
		SQLWithParams swp = DialectSyntaxParser.getParser().getPaginationSQLWithParams(SQL_GET_EVETS_BY_USER_OPTR,
				new Object[] { userUid, optrType }, start, pageSize);
		return dbe.list(swp.getSql(), UserEvent.class, swp.getParams());
	}

}